package com.yeyaomai.dksns.flowis.page;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ReflectionUtils;

/**
 * 
 * @author dingtao@yeyaomai.com
 * 
 *         覆盖SpringData 中 content属性
 * 
 * @param <T>
 */
public class PageImplResult<T> extends PageImpl<T> {

	public static Logger logger = LoggerFactory.getLogger(PageImplResult.class);

	private static final long serialVersionUID = 5280347771904266113L;

	public boolean flag;

	/**
	 * 
	 * @param content
	 * @param pageable
	 * @param total
	 */
	@SuppressWarnings("all")
	public PageImplResult(List<T> content, Pageable pageable, long total) {

		super(Collections.EMPTY_LIST, pageable, total);
		setParentContent(content);

	}

	/**
	 * 
	 * @param args
	 *            把查询的结果 放到SpringData 中的 content
	 */
	public void setParentContent(List<T> args) {
		try {
			Field field = ReflectionUtils.findField(PageImpl.class, "content");
			field.setAccessible(true);
			field.set(this, args);
		} catch (Exception e) {
			logger.error(e.toString());

		}

		flag = true;

	}
	
	/**
	 *  下一页
	 * @return
	 */
	public int nextPage()
	{
		
		if(hasNextPage())
		{
		   return this.getNumber() +1;
		}
		
		return getTotalPages();
		
	}

	/**
	 *  上一页
	 * @return
	 */
	public int frontPage()
	{
		if(hasPreviousPage())
		{
			return this.getNumber() -1;
		}
		return 1;
		
	}
	/**
	 * 当前页 大于 1 有上一页
	 */
	public boolean hasPreviousPage() {

		return  getNumberOfElements() >0 && getNumber() > 1;
	}

    /**
     *   是否是首页
     */
	public boolean isFirstPage() {

		return  !hasPreviousPage() && getNumberOfElements() >0;
	}

	/**
	 * 是否有下一页
	 */
	public boolean hasNextPage() {

		return   getNumberOfElements()>0 &&  getNumber()< getTotalPages();
	}


	/**
	 *  是否是末页
	 */
	public boolean isLastPage() {

		return !hasNextPage() && getNumberOfElements() >0 ;
	}

}
